package gov.mogaha.ntis.cmm.mob.adapter;

import gov.mogaha.ntis.cmm.mob.R;
import gov.mogaha.ntis.cmm.mob.dto.CmmMobSidCnDto;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CmmMobSidCnAdpater extends BaseAdapter {
	private final LayoutInflater layoutInflater;
	private final ArrayList<CmmMobSidCnDto> list;
	
	public CmmMobSidCnAdpater(Context context, ArrayList<CmmMobSidCnDto> list) {
		this.layoutInflater = LayoutInflater.from(context);
		this.list = list;
	}
	
	public ArrayList<CmmMobSidCnDto> getList() {
		return list;
	}

	public int getCount() {
		return list.size();
	}

	public Object getItem(int position) {
		return list.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder viewHolder;
		CmmMobSidCnDto tmp = list.get(position);
		
		if (convertView == null) {
			
			viewHolder = new ViewHolder();
			convertView = this.layoutInflater.inflate(R.layout.cmmmobsidcnitemview, null);
			
			viewHolder.trgtNmText = (TextView) convertView.findViewById(R.id.trgt_nm_text);
			viewHolder.trgtSidText = (TextView) convertView.findViewById(R.id.trgt_sid_text);
			viewHolder.trgtAddrText = (TextView) convertView.findViewById(R.id.trgt_addr_text);

			viewHolder.upchLayout = (LinearLayout) convertView.findViewById(R.id.upch_layout);
			viewHolder.upchNmText = (TextView) convertView.findViewById(R.id.upch_nm_text);
			viewHolder.tgtApvNoText = (TextView) convertView.findViewById(R.id.tgt_apv_no_text);
			viewHolder.upchAddrText = (TextView) convertView.findViewById(R.id.upch_addr_text);
			
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		viewHolder.trgtNmText.setText(tmp.mTrgtNm);
		viewHolder.trgtSidText.setText(tmp.mConvertSid);
		viewHolder.trgtAddrText.setText(tmp.mTrgtAddr);
		
		viewHolder.upchNmText.setText(tmp.mUpchNm);
		viewHolder.tgtApvNoText.setText(tmp.mTgtApvNo);
		viewHolder.upchAddrText.setText(tmp.mUpchAddr);
		
		if(tmp.mTgtApvNo.length() > 0) {
			viewHolder.upchLayout.setVisibility(View.VISIBLE);
		} else {
			viewHolder.upchLayout.setVisibility(View.GONE);
		}
 
		convertView.setTag(viewHolder);
		
		return convertView;
	}
	
	private class ViewHolder {
		TextView trgtNmText;
		TextView trgtSidText;
		TextView trgtAddrText;
		
		LinearLayout upchLayout;
		TextView upchNmText;
		TextView tgtApvNoText;
		TextView upchAddrText;
	}
}
